import { PlusOutlined } from '@ant-design/icons';
import type { ActionType } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { Button } from 'antd';
import { useRef, useState } from 'react';
import AddModal from '@/components/Modal/AddModal';
import AddExam from '@/components/AddExam';
import getColumns from './columns';
import { FileAdd } from '../../medicalHistory/mzgl/form'
import { proTableFormat, sortTransform, } from '@/utils/format'
import { getEntryExamList, uploadResult } from '@/services/entryExam'
import WithAuthComp from '@/components/WithAuthComp';
import { ROLE } from '@/common/const'

export default () => {
    const [readonly, setReadonly] = useState<boolean>(false)
    const [id, setId] = useState<string | null>(null)
    const dialogRef = useRef<any>()
    const actionRef = useRef<ActionType>();
    const resultAddRef = useRef()
    const columns = getColumns(dialogRef, setReadonly, setId, resultAddRef);

    const onAddFinish = () => {
        dialogRef.current.hideModelRef()
        actionRef.current?.reload(true)
    }
    return (
        <>
            <ProTable
                columns={columns}
                cardBordered
                actionRef={actionRef}
                request={async (params, sort) => {
                    const { pageInfo } = await getEntryExamList({ ...params, currentPage: params.current, sort: sortTransform(sort), type: 'ndtj' });
                    return proTableFormat(pageInfo)
                }}
                rowKey="id"
                search={{
                    labelWidth: 'auto',
                    span: 6
                }}
                options={{
                    setting: true,
                    reload: false
                }}
                pagination={{
                    pageSize: 10,
                    onChange: (page) => console.log(page),
                }}
                dateFormatter="string"
                // 表格新建栏
                toolBarRender={() => [
                    <WithAuthComp authKey={[ROLE.YS, ROLE.HS, ROLE.ZL]}>
                        <Button
                            key="button"
                            icon={<PlusOutlined />}
                            onClick={() => {
                                setReadonly(false)
                                setId(null)
                                dialogRef?.current?.showModelRef()
                            }}
                            type="primary"
                        >
                            新建
                        </Button>
                    </WithAuthComp>
                    ,
                ]}
            />
            <AddModal ref={dialogRef} title='年度体检' width="80%" style={{ top: 20 }}>
                <AddExam readonly={readonly} id={id} onSave={onAddFinish} type='ndtj' />
            </AddModal>
            {/* 体检结果 */}
            <AddModal ref={resultAddRef} title='上传体检结果' width='600px'>
                <FileAdd id={id} uploadApi={uploadResult} type='tjjg' />
            </AddModal>
        </>

    );
};